package com.hls.zfbai.modules.common.util;

import com.hls.zfbai.core.utils.StringUtils;
import com.hls.zfbai.modules.common.dao.CommonMapper;
import com.hls.zfbai.modules.common.entity.CommonMaxIdParam;
import com.hls.zfbai.modules.common.entity.GyDmzd;
import com.hls.zfbai.modules.common.entity.GyXtcs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class CommonUtils {
    private static CommonMapper commonMapper;

    @Autowired
    private CommonUtils(CommonMapper commonMapper) {
        this.commonMapper = commonMapper;
    }

    /**
     * 获取主键序列
     * @param commonMaxIdParam
     * @return
     */
    public static Long getSequence(CommonMaxIdParam commonMaxIdParam) {
        commonMapper.bsoftProGetMax(commonMaxIdParam);
        return commonMaxIdParam.getDqz();
    }

    /**
     * 获取主键序列
     * @param tableName
     * @param identity
     * @param count
     * @return
     */
    public static Long getSequence(String tableName, String identity, Integer count) {
        CommonMaxIdParam commonMaxIdParam = new CommonMaxIdParam();
        commonMaxIdParam.setTableName(tableName);
        commonMaxIdParam.setIdentity(identity);
        commonMaxIdParam.setCount(count);
        return getSequence(commonMaxIdParam);
    }

    /**
     * 获取主键序列
     * @param tableName
     * @param count
     * @return
     */
    public static Long getSequence(String tableName, Integer count) {
        CommonMaxIdParam commonMaxIdParam = new CommonMaxIdParam();
        commonMaxIdParam.setTableName(tableName);
        if(StringUtils.isNotBlank(tableName) && tableName.indexOf("_") > 0) {
            commonMaxIdParam.setIdentity("GY_IDENTITY_" + tableName.substring(0, tableName.indexOf("_")));
        }
        commonMaxIdParam.setCount(count);
        return getSequence(commonMaxIdParam);
    }

    /**
     * 获取主键序列
     * @param tableName
     * @return
     */
    public static Long getSequence(String tableName) {
        return getSequence(tableName, 1);
    }

    /**
     * 获取公用系统参数
     * @param JGID
     * @param CSMC
     * @param MRZ
     * @param BZ
     * @return
     */
    public static String getGyXtcs(String JGID, String CSMC, String MRZ, String BZ) {
        GyXtcs gyXtcs = new GyXtcs(JGID, CSMC, MRZ, BZ);
        commonMapper.getXtcs(gyXtcs);
        return gyXtcs.getCSZ();
    }

    /**
     * 根据DMLB获取字典列表
     * @param dmlb
     * @return
     */
    public static List<GyDmzd> getGyDmzdList(Long dmlb) {
        GyDmzd gyDmzd = new GyDmzd(dmlb);
        List<GyDmzd> dmzdList = commonMapper.getGyDmzdList(gyDmzd);
        return dmzdList;
    }

    /**
     * 根据DMLB和DMSB获取代码信息
     * @param dmlb
     * @param dmsb
     * @return
     */
    public static GyDmzd getGyDmzd(Long dmlb, Long dmsb) {
        GyDmzd gyDmzd = new GyDmzd(dmlb, dmsb);
        GyDmzd dmzdInfo = commonMapper.getGyDmzd(gyDmzd);
        return dmzdInfo;
    }
}